package org.wzq;

import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-transaction.xml");
        // AOP事务
        Product productA = new Product("codeA", "descA");
        IProductDao productDaoProxy = (IProductDao) ctx.getBean("productDaoProxy");
        productDaoProxy.save(productA);

        // 无事务-情景1
        // report: No Session Found For Current Thread
        // The sessionFactory needs transaction to work,
        // to solve it, declare a transaction manager in Spring
        Product productB = new Product("codeB", "descB");
//        IProductDao productDao = (IProductDao) ctx.getBean("productDao");
//        productDao.save(productB);

        // report: No Session Found For Current Thread
        // 无事务-情景2
        SessionFactory sf = (SessionFactory) ctx.getBean("sessionFactory");
        sf.getCurrentSession().save(productB);
    }
}
